﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MimiDouban.Abstract;
using System.Windows.Media.Animation;

namespace MimiDouban.UI
{
    /// <summary>
    /// Lyric.xaml 的交互逻辑
    /// </summary>
    public partial class Lyric : Window, IOutputUI
    {
        //主窗口引用
        private Window mainWindow;
        public Lyric(Window mainWindow)
        {
            InitializeComponent();
            this.mainWindow = mainWindow;
        }

        private Storyboard story;
        private string lastLyric;
        DoubleAnimation da = null;


        #region IOutputUI 成员

        void IOutputUI.start()
        {
            //throw new NotImplementedException();
        }

        void IOutputUI.ShowSongInfo(MimiDouban.Abstract.Beans.DoubanSong song)
        {
            //throw new NotImplementedException();
        }

        void IOutputUI.ShowChannel(MimiDouban.Abstract.Beans.DoubanChannel channel)
        {
            //throw new NotImplementedException();
        }

        void IOutputUI.ShowNextSong(MimiDouban.Abstract.Beans.DoubanSong song)
        {
            //throw new NotImplementedException();
        }

        void IOutputUI.showTime(int min, int sec)
        {
            //throw new NotImplementedException();
        }

        void IOutputUI.showBuffer(string str, int currentTime)
        {
            //throw new NotImplementedException();
        }

        void IOutputUI.showLyric(string str, int currentTime, TimeSpan dur)
        {
            currentLyric.Text = str;
            if (str == null || str == "")
            {
                Hide();
            }
            else
            {
                if (lastLyric != str)
                {
                    showMe(dur);
                    lastLyric = str;
                }
            }
        }

        void IOutputUI.setLikeStatus(ControlStatus status)
        {
            //throw new NotImplementedException();
        }

        ControlStatus IOutputUI.getLikeStatus()
        {
            //throw new NotImplementedException();
            return ControlStatus.Enabled;
        }

        void IOutputUI.setDislikeStatus(ControlStatus status)
        {
            //throw new NotImplementedException();
        }

        void IOutputUI.setPauseStatus(bool paused)
        {
            //throw new NotImplementedException();
            //throw new NotImplementedException();
            //把上一次动画暂停下来
            try
            {
                if (paused)
                {
                    if (story != null && story.GetCurrentState() == ClockState.Active && story.GetIsPaused() == false)
                    {
                        story.Pause();
                    }
                }
                else
                {
                    if (story != null && story.GetCurrentState() == ClockState.Active && story.GetIsPaused() == true)
                    {
                        story.Resume();
                    }
                }
            }
            catch (System.Exception ex)
            {
                MimiDouban.Test.TestLog.getInstance().error("lyrics setPauseStatus catch", ex);

            }
        }

        void IOutputUI.showLogin()
        {
            //throw new NotImplementedException();
        }

        void IOutputUI.showCurrentUser(string username)
        {
            //throw new NotImplementedException();
        }

        void IOutputUI.showLoginBtn()
        {
            //throw new NotImplementedException();
        }

        void IOutputUI.showVolume(double volume)
        {
            //throw new NotImplementedException();
        }

        MimiDouban.Abstract.Beans.DoubanChannel[] IOutputUI.Channels
        {
            get
            {
                //throw new NotImplementedException();
                return null;
            }
            set
            {
                //throw new NotImplementedException();
            }
        }

        void IOutputUI.end()
        {
            //throw new NotImplementedException();
            this.Close();
        }

        #endregion

        private void showMe(TimeSpan dur)
        {
            double screenHeight = SystemParameters.PrimaryScreenHeight;
            double screenWidth = SystemParameters.PrimaryScreenWidth;
            this.Top = screenHeight - this.Height - 50;
            this.Left = screenWidth/2 - this.Width/2;

            //先把上一次动画停下来
            if (story != null && story.GetCurrentState() == ClockState.Active)
            {
                story.Stop();
            }
            
            //开始动画
            this.Show();
            try
            {
                //Resources["AnimationTime"] = "0:0:4";
                if ( da == null )
                {
                	da = new DoubleAnimation(0.0, 1.0, new Duration(dur));
                }
                else
                {
                    da.Duration = new Duration(dur);
                }
                story.Children.Clear();
                story.Children.Add(da);
            }
            catch (System.Exception ex)
            {

            }
            story = (Resources["karaokeTextEffect"] as Storyboard);
            story.Begin();
        }
    }
}
